package com.ocl.www.controller;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.ocl.www.service.UserFansService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.log4j.Logger;
import org.apache.log4j.spi.LoggerFactory;
import org.apache.shiro.cache.ehcache.EhCacheManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.ocl.www.basic.BasicController;
import com.ocl.www.domain.Article;
import com.ocl.www.domain.ArticleImages;
import com.ocl.www.domain.Category;
import com.ocl.www.domain.User;
import com.ocl.www.service.ArticleImageService;
import com.ocl.www.service.ArticleService;
import com.ocl.www.service.CategoryService;

import javax.servlet.http.HttpSession;


@RestController
@RequestMapping("/article")
public class ArticleController extends BasicController {

	@Autowired
	private ArticleService articleService;
	@Autowired
	private EhCacheManager em;

	@Autowired
	private ArticleImageService arts;

	private Logger log = Logger.getLogger(ArticleController.class);


	@ResponseBody
	@RequestMapping(value = "/selectArticles", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	@ApiOperation(value="查询指定作者所有的文章", notes="根据ID查询指定作者所有的文章，")
	@ApiImplicitParam(name = "maptemp", value = "使用此格式：{}", required = true, dataType = "java.util.Map")
	public List<Article> selectArticles(@RequestBody Map<String,Object> maptemp, HttpSession httpSession){
		log.info(httpSession.getId()+"===myname is "+httpSession.getAttribute("myname"));
		List<Article> list = null;
		
		Article art = new Article();
		art.setAuthor(Integer.valueOf(maptemp.get("userId").toString()));
		
		try{
			
		  list = articleService.selectArticles(art);
		  
		}catch(Exception e){
			
			e.printStackTrace();
			
		}
		return list;
	}

	@ApiOperation(value="添加文章", notes="添加作者的文章，")
	@ApiImplicitParam(name = "articleinfo", value = "使用此格式：{}", required = true, dataType = "Map")
	@RequestMapping(value = "/insertarticle", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public int insertarticle (@RequestBody Map<String,String> articleinfo) {
		int detailId = 0;
		Date d = new Date();
		String[] imgstrarr = articleinfo.get("imgarr").split(",");
		
		int count =  0;
		
		Article art = new Article();
//		art.setAuthor((int)em.getCache("shiroCache").get("user_id"));
		art.setAuthor(Integer.valueOf(articleinfo.get("userId").toString()));
		art.setMainPicture(imgstrarr[0]);
		art.setAddressip(articleinfo.get("addressip"));
		art.setTagcontent(articleinfo.get("tagcontent"));
		art.setCreateTime(new Timestamp(d.getTime()));
		art.setArticlecontent(articleinfo.get("aticlecontent"));
		art.setTitle(articleinfo.get("title"));
		try {
//			System.out.println("======================"+request.getParameter("userName"));
			count = (int) articleService.insertArticle(art);
			detailId = art.getArticleId();
			System.out.println("添加成功数:"+count+",主键为："+art.getArticleId());
			
			//添加图片
			for(int i = 0;i<imgstrarr.length;i++){
				
				ArticleImages at = new ArticleImages();
				at.setImageUrl(imgstrarr[i]);
				at.setDetailId(detailId);
				at.setCreateTime(new Timestamp(d.getTime()));
				arts.insertArticleImage(at);
				
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		
		return count;
	}


	@ApiOperation(value="查询所有文章", notes="查询所有文章，")
	@RequestMapping(value = "/queryArticles", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public List<Article> queryArticles(){

		List<Article> list = new ArrayList<Article>();
		try{

			list = articleService.queryArticles();

		}catch(Exception e){

			e.printStackTrace();

		}

		return list;
	}

}
